package com.sunshuo.springsecuritydemo.service;

import com.sunshuo.springsecuritydemo.dao.RoleMenuDao;
import com.sunshuo.springsecuritydemo.pojo.RoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName RoleMenuService
 * @Description TODO
 * @Author sunshuo
 */
@Service
public class RoleMenuService {

    private RoleMenuDao roleMenuDao;

    @Autowired
    public void setRoleMenuDao(RoleMenuDao roleMenuDao) {
        this.roleMenuDao = roleMenuDao;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateRelations(String roleId, List<RoleMenu> roleMenus) throws Exception {
        try {
            roleMenuDao.deleteByRoleId(roleId);
            roleMenus.forEach(roleMenu -> {
                roleMenuDao.add(roleMenu);
            });
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
